import { ApiResponse } from '@/types/data'
import { ArticleDetail, CommentRes, RootThunkAction } from '@/types/store'
import request from '@/utils/http'

export function getArticleInfo(id: string): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<ArticleDetail>>(`/articles/${id}`)
    console.log(res.data.data)
    dispatch({
      type: 'article/setArticleInfo',
      payload: res.data.data,
    })
  }
}

// 获取文章的评论
export function getCommentList(id: string): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<CommentRes>>('/comments', {
      params: {
        type: 'a',
        source: id,
      },
    })
    console.log(res.data.data)
    dispatch({
      type: 'article/saveComment',
      payload: res.data.data,
    })
  }
}